<#--CONTROLLER-->
<#-- 传入参数 Func func, DataTable table --> 
package ${func.javaPkg}.controller;

import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.vecspace.data.model.EntityBO;
import com.vecspace.data.model.ListExBO;
import com.vecspace.data.model.FilterColumn;
import com.vecspace.dicttable.DictAll;
import com.vecspace.jsmodel.ZTreeNode;
import com.vecspace.msoffice.ExcelTemplate;
import com.vecspace.utils.Convert;
import com.vecspace.web.servlet.model.BaseController;
import com.vecspace.web.servlet.model.HController;
import com.vecspace.web.servlet.model.Param;
import com.vecspace.web.servlet.model.Path;
import com.vecspace.constant.AuthTag;
import com.vecspace.database.model.QueryConfig;
import ${func.javaPkg}.pojo.${table.className};
import ${func.javaPkg}.pojo.${table.className}Ex;
import ${func.javaPkg}.pojo.${table.className}Stats;
import ${func.javaPkg}.service.${table.className}Service;

/** ${table.tableCaption!} Controller
 * 在Controller中进行与页面相关的处理，比如把数据转换为HTML格式等。
 * @template dayu-code-builder
 * @author ${author}
 * @reviewer
 * @createTime ${nowTime}
 * @version 1.0
*/
@HController("${table.beanName}")
public class ${table.className}Controller extends BaseController {

    /**根据ID查询 ${table.TableDesc!}
     * @return ${table.className}
     * @throws Exception
     * @author ${author}
     * @reviewer 
     * @createTime ${nowTime}
     */
    @Path("get${table.className}")
    public ${table.className} get${table.className}(@Param("${table.idVarName}") ${table.idPropJavaType} ${table.idVarName}) throws Exception {
        return ${table.beanName}Service.get${table.className}(${table.idVarName});
    }
    
    @Path("get${table.className}BO")
    public EntityBO<${table.className}> get${table.className}BO(@Param("${table.idVarName}") ${table.idPropJavaType} ${table.idVarName}) throws Exception {
        ${table.className} ${table.beanName} = ${table.beanName}Service.get${table.className}(${table.idVarName});
        EntityBO<${table.className}> entityBO = new EntityBO<${table.className}>(${table.beanName});
<#list columns as column>
  <#if (column.codeType!'')?length gt 0>
        entityBO.addDictCodeType("${column.varName}", DictAll.getItems(${table.className}.${column.propName}Code));
  </#if>
</#list>
        return entityBO;
    }

<#if parentColumn??>
    /**根据父节点id获取子节点
     *
     * @param nodeId ztree的父节点id，znode.id
     * @return
     * @throws Exception
     */
    @Path(value = "getChildNodes")
    public List<ZTreeNode> getChildNodes(<#if foreignColumn??>@Param("${foreignColumn.varName}")${foreignColumn.javaType} ${foreignColumn.varName}, </#if>@Param("pId")String nodeId<#if leafTable?? && showLeaf>, @Param("showLeaf")boolean showLeaf</#if>) throws Exception {
<#if table.idPropJavaType == "Long">
    	${table.idPropJavaType} topId = -1L;
<#elseif table.idPropJavaType == "Integer">
    	${table.idPropJavaType} topId = -1;
<#else>
        ${table.idPropJavaType} topId = "-1";
</#if>
        ${table.idPropJavaType} ${parentColumn.varName} = topId;
        String nodeType = "cat";//nodeId的前缀
        if(StringUtils.isBlank(nodeId)){
            nodeType = "cat";
            ${parentColumn.varName} = topId;
        }else{
            int spIdx = nodeId.indexOf("-");
            if(spIdx >= 0){
                nodeType = nodeId.substring(0, spIdx);
<#if table.idPropJavaType == "Long">
                ${parentColumn.varName} = Convert.toLong(nodeId.substring(spIdx+1), -1L);
<#elseif table.idPropJavaType == "Integer">
                ${parentColumn.varName} = Convert.toInteger(nodeId.substring(spIdx+1), -1);
<#else>
                ${parentColumn.varName} = nodeId.substring(spIdx+1), -1L;
</#if>
            }else{
                nodeType = "cat";
<#if table.idPropJavaType == "Long">
                ${parentColumn.varName} = Convert.toLong(nodeId, -1L);
<#elseif table.idPropJavaType == "Integer">
                ${parentColumn.varName} = Convert.toInteger(nodeId, -1);
<#else>
                ${parentColumn.varName} = nodeId;
</#if>
            }
        }

        List<ZTreeNode> znodes = new ArrayList<>();
        if("cat".equals(nodeType)) {
            //获取下级节点
            List<${table.className}> ${table.beanName}List = ${table.beanName}Service.getChild${table.className}List(<#if foreignColumn??>${foreignColumn.varName},</#if>${parentColumn.varName});
            for(${table.className} ${table.beanName} : ${table.beanName}List){
                ZTreeNode znode = new ZTreeNode();
                //对id增加前缀，区分不同的数据对象
                znode.setId("cat-"+${table.beanName}.get${table.idPropName}());
                znode.setpId(nodeId);
                znode.setName(${table.beanName}.get${(table.nameColumn.propName)!'Name'}());
                znode.setIsParent(true);
                znode.setOrdinalNumber(${table.beanName}.get${(table.orderColumn.propName)!'Name'}());
                znodes.add(znode);
            }

<#if leafTable?? && showLeaf>
            if(showLeaf){
                //获取关联的叶子节点数据
                List<${leafTable.className}> ${leafTable.beanName}List = ${leafTable.beanName}Service.get${leafTable.className}ListBy${(table.className)!''}(${parentColumn.varName});
                for(${leafTable.className} ${leafTable.beanName} : ${leafTable.beanName}List){
                    ZTreeNode znode = new ZTreeNode();
                    //对id增加前缀，区分不同的数据对象
                    znode.setId("leaf-"+${leafTable.beanName}.get${table.idPropName}());
                    znode.setpId(nodeId);
                    znode.setName(${leafTable.beanName}.get${(table.nameColumn.propName)!'Name'}());
                    znode.setIsParent(false);
                    znode.setOrdinalNumber(${leafTable.beanName}.get${(table.orderColumn.propName)!'Name'}());
                    znodes.add(znode);
                }
            }
</#if>
        }
        return znodes;
    }
</#if>

    /** 插入或修改 ${table.TableDesc!} 
     * @param item    要保存的对象
     * @return void
     * @throws Exception
     * @author ${author}
     * @reviewer
     * @createTime ${nowTime}
     */
    @Path("save${table.className}")
    public EntityBO<${table.className}> save${table.className}(@Param("item") ${table.className} item) throws Exception {
        //TODO：增加权限判断，是否可以新增、编辑
        if (item == null){
            return null;
        }
        /*** 对item对象进行校验，如必填项，格式，逻辑关系等 ***/
        ${table.className} ${table.beanName} = ${table.beanName}Service.saveView${table.className}(item);
        return get${table.className}BO(${table.beanName}.get${table.idColumn.propName}());
    }

<#if updateCellValueEnable==true>
    /**更新单个字段的值
     *
     * @param ${table.idVarName} 记录id
     * @param propName 属性名称
     * @param value 属性值
     * @return
     * @throws Exception
     * @author ${author}
     * @reviewer
     * @createTime ${nowTime}
    */
    @Path("updateProp")
    public void updateProp(@Param("${table.idVarName}")${table.idPropJavaType} ${table.idVarName}, @Param("propName")String propName, @Param("value")Object value) throws Exception {
        //TODO：增加权限判断，是否可以编辑
        ${table.beanName}Service.updateValueByProp(${table.idVarName}, propName, value);
    }
</#if>


<#if isTree==true && parentColumn??>
    /**更改 自己所属的父节点
     *
     * @param ${table.idVarName} ${table.tableName}.${table.idVarName}
     * @param ${parentColumn.varName}New 新的 ${parentColumn.columnCaption}
     * @throws Exception
     * @author ${author}
     * @reviewer
     * @createTime ${nowTime}
     */
    @Path("change${parentColumn.propNameTrimId}")
    public void change${parentColumn.propNameTrimId}(@Param("${table.idVarName}")${table.idPropJavaType} ${table.idVarName}, @Param("${parentColumn.varName}New")${parentColumn.javaType} ${parentColumn.varName}New) throws Exception {
        ${table.beanName}Service.change${table.parentColumn.propNameTrimId}(${table.idVarName}, ${parentColumn.varName}New);
    }
</#if>

<#if table.isLockByRow>
    /**修改数据锁定状态
     * @param id 记录id，另-1表示对整表进行锁定
     * @param lockState 锁定状态，参考 DataLockState
     * @date 2021/11/17  20:45
     * @return void
     **/
    @Path("changeLockState")
	public void changeLockState(@Param("id")${table.idPropJavaType} id, @Param("lockState")int lockState) throws Exception {
        int authTag = AuthTag.All;//从权限系统获取操作权限类型
        ${table.beanName}Service.changeLockState(id, lockState, authTag);
    }
</#if>
<#if table.isLockByMaster>
    /**<#if foreignColumn??>修改数据被主数据的锁定状态<#else>修改整表被锁定的状态</#if>
     <#if foreignColumn??>* @param ${foreignColumn.varName} 主数据记录id，另-1表示对${table.className}整表进行锁定</#if>
     * @param lockState 锁定状态，参考 DataLockState
     * @date 2021/11/17  20:45
     * @return void
     **/
    @Path("changeMasterLockState")
    public void changeMasterLockState(<#if foreignColumn??>@Param("${foreignColumn.varName}")${foreignColumn.javaType} ${foreignColumn.varName}, </#if>@Param("lockState")int lockState) throws Exception {
        int authTag = AuthTag.All;//从权限系统获取操作权限类型
        ${table.beanName}Service.changeMasterLockState(<#if foreignColumn??>${foreignColumn.varName}, </#if>lockState, authTag);
    }
</#if>

    /** 根据ID删除 ${table.TableDesc!}
     * @param ${table.idVarName}    主键值
     * @return void
     * @throws Exception
     * @author ${author}
     * @reviewer
     * @createTime ${nowTime}
     */
    @Path("delete${table.className}")
    public void delete${table.className}(@Param("${table.idVarName}") ${table.idPropJavaType} ${table.idVarName}) throws Exception {
        //TODO：增加权限判断，是否可以删除
        ${table.beanName}Service.delete${table.className}(${table.idVarName});
    }

    /**根据查询条件获取查询结果（分页查询），同时进行聚合统计查询
     * 
     <#if foreignColumn??>* @param ${foreignColumn.varName} ${foreignColumn.caption!} </#if>
     <#if useRecurseQuery==true>* @param recursion 是否递归查询所有下级数据 </#if>
     * @param filters    查询条件（字段(属性)和值）
     * @param queryConfig  分页和排序参数
     * @return PageModel
     * @throws Exception
     * @author
     * @createTime 2021-10-05 23:16:31
     */
    @ExcelTemplate(templateFileName = "${table.beanName}.xlsx")
    @Path("get${table.className}ExListAndStats")
    public ListExBO<${table.className}Ex, Void, ${table.className}Stats> get${table.className}ExListAndStats(<#rt>
        <#lt><#if foreignColumn??>@Param("${foreignColumn.varName}")${foreignColumn.javaType} ${foreignColumn.varName}, </#if><#rt>
        <#lt><#if useRecurseQuery==true>@Param("recursion")boolean recursion, </#if><#rt>
        <#lt>@Param("filters") ArrayList<FilterColumn> filters,
          @Param("queryConfig") QueryConfig queryConfig) throws Exception {

        ListExBO<${table.className}Ex, Void, ${table.className}Stats> ${table.beanName}BOList = ${table.beanName}Service.get${table.className}ExListAndStats(<#if foreignColumn??>${foreignColumn.varName}, </#if><#if useRecurseQuery==true>recursion, </#if>filters, queryConfig.getPageIndex(), queryConfig.getPageSize(), queryConfig.getSortName(), queryConfig.getSortOrder());

<#list table.columns as column>
  <#if (column.codeType!'')?length gt 0>
        ${table.beanName}BOList.addDictCodeType("${column.varName}", DictAll.getItems(${table.className}.${column.propName}Code));
  </#if>
</#list>

        return ${table.beanName}BOList;
    }
    
    /************* property ********************/
    @Autowired
    private ${table.className}Service ${table.beanName}Service;

<#if leafTable?? && showLeaf>
    @Autowired
    private ${leafTable.className}Service ${leafTable.beanName}Service;
</#if>

}
